/*自定义一个string类型，要求有合理的构造函数，拷贝构造方法、析构函数，输出方法；
class String{
private:
    char *_data;
public:
    String(const char *str  = 0);
    String(const String& rhs);
    ~String();
    const char* data() const;
};
*/
#include <iostream>
using namespace std;
#include <cstring>
#include <cstdlib>

class String{
private:
    char *_data;
public:
    //构造
    String(const char *str)
    {
        if(0 == str)
        {
            this->_data = new char[1]();
            this->_data[0] = '\0';
        }
        else
        {
            int len = strlen(str);
            this->_data = new char[len + 1]();
            strcpy(this->_data, str);
            this->_data[len] = '\0';
	    }
    }
    //拷贝构造
    String(const String& rhs)
    {
        int len = strlen(rhs._data);
        this->_data = new char[len + 1]();
        strcpy(this->_data, rhs._data);
        this->_data[len] = '\0';
    }
    //析构
    ~String()
    {
        if(this->_data != 0)
		delete [] this->_data;
    }
    //输出
    void outPut()
    {
        cout << this->data() << endl;
    }
	const char* data() const { return this->_data; }
};

int main(int argc,char*argv[])
{
    String str1("hello");
    cout<<"str1:";
    str1.outPut();

    String str2(str1);
    cout<<"str2:";
    str2.outPut();

    return 0;
}